package alingrad.engine.src.gameobjects;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;

import alingrad.engine.src.core.TreeNode;

/**
 * Class that manages all game objects. Higher node in the Game Tree.
 * @author alingrad
 *
 */
public class GameObjectManager extends TreeNode {

	/**
	 * List of all game objects.
	 */
	private LinkedList<GameObject> gameObjects;

	/**
	 * Constructs a Game Object Manager
	 */
	public GameObjectManager() {
	}

	/**
	 * Loads all objects managed.
	 * @throws IOException
	 */
	public static void load() throws IOException {
		Rectangle.load();
	}

	@Override
	public void initialize() {
		gameObjects = new LinkedList<GameObject>();
		Rectangle rect1 = new Rectangle(10, 10);
		Rectangle rect2 = new Rectangle(100, 50);
		rect1.setOther(rect2);
		rect2.setOther(rect1);
		gameObjects.add(rect1);
		gameObjects.add(rect2);
		for (GameObject object : gameObjects) {
			object.initialize();
		}
	}

	@Override
	public void update(long delta) {
		Iterator<GameObject> iter = gameObjects.iterator();
		GameObject object;
		while (iter.hasNext()) {
			object = iter.next();
			if (object.isValid)
				object.update(delta);
			else {
				object.destroy();
				iter.remove();
			}
		}
	}
}
